//
//  MSHomeBottomView.m
//  MS
//
//  Created by Yuan on 13-2-9.
//  Copyright (c) 2013年 He Yuan. All rights reserved.
//

#import "MSHomeBottomView.h"
#import "UIColor+ColorUtils.h"

@implementation MSHomeBottomView

- (id)initWithFrame:(CGRect)frame withDelegate:(id)delegate
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self setBackgroundColor:[UIColor clearColor]];
        [self setContentMode:UIViewContentModeBottomLeft];
        
        NSArray *configs = [[MSSetting sharedSetting]homePageBottomViewConfig];
        NSInteger totals = [configs count];
        NSInteger rows = 2;
        NSInteger cols = 3;
        CGFloat btn_width = 100;
        CGFloat space_x = ([UIScreen mainScreen].applicationFrame.size.width - cols*btn_width)/(cols+1);
        CGFloat space_y = space_x;
        CGFloat btn_height = (frame.size.height - space_y*(rows+1))/rows;
        CGFloat offset_x = 0.0;
        CGFloat offset_y = 0.0;
        for (int row = 0 ; row < rows; row++)
        {
            for (int col = 0 ; col < cols ; col++)
            {
                NSInteger idx = row*cols + col;
                if (idx >= totals)break;
                NSDictionary *item = [configs objectAtIndex:idx];
                offset_x = space_x + (btn_width + space_x)*col;
                offset_y = space_y + (btn_height + space_y)*row;
                SEL sel = NSSelectorFromString([item objectForKey:@"action"]);
                UIButton *imgbtn = [MSTool createButton:CGRectMake(offset_x,offset_y,btn_width,btn_height) action:sel delegate:delegate];
                UIImage *hometab = [UIImage imageNamed:[NSString stringWithFormat:@"home_tab_%d",idx+1]];
                [imgbtn setContentMode:UIViewContentModeScaleAspectFit];
                [imgbtn setBackgroundColor:[UIColor colorWithString:[item objectForKey:@"color"]]];
                [imgbtn setImage:hometab forState:UIControlStateNormal];
                [imgbtn setImage:hometab forState:UIControlStateHighlighted];
                UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(0,0, btn_width,30)];
                CGPoint center = imgbtn.imageView.center;
                center.y += 35;
                lbl.center = center;
                [lbl setFont:FONT_NORMAL_BOLD];
                [lbl setText:[item objectForKey:@"title"]];
                [lbl setBackgroundColor:[UIColor clearColor]];
                [lbl setTextAlignment:UITextAlignmentCenter];
                [lbl setTextColor:[UIColor whiteColor]];
                [imgbtn addSubview:lbl];
                [self addSubview:imgbtn];
            }
        }
        
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
